package com.eazyimage.lab.controller;


import com.eazyimage.lab.service.DatasetMessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@RequestMapping("taskManage")
public class FileUpload {

    @PostMapping("uploadFile")
    public Object multipleSave(@RequestParam("file") MultipartFile file){
        File file3 = new File("F:\\finish\\testing1\\velodyne\\" + "pcd_1000.bin");
        if(file3.isFile() && file3.exists()){
            file3.delete();
        }

        OutputStream os = null;
        InputStream inputStream = null;
        String fileName = null;
        try {
            inputStream = file.getInputStream();
            fileName = file.getOriginalFilename();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String path = "F:\\finish\\testing1\\velodyne";
            // 二、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕，关闭全部连接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

//        System.out.println(fileName);
        File file2 = new File("F:\\finish\\testing1\\velodyne\\" + fileName);
        String file2name = file2.getAbsolutePath();
        if(file2name.indexOf(".")>=0){
            file2name = file2name.substring(0,file2name.lastIndexOf("."));
        }
        file2.renameTo(new File("F:\\finish\\testing1\\velodyne\\" + "pcd_1000.bin"));

        String file1 = "F:\\finish\\runtest.bat";
        boolean isCloseWindow = true;
        String cmdCommand = null;
        if (isCloseWindow) {
            cmdCommand = "cmd.exe /c " + file1;
        } else {
            cmdCommand = "cmd.exe /k " + file1;
        }
        StringBuilder stringBuilder = new StringBuilder();
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(cmdCommand);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), "GBK"));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append(" ");
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
